local file = io.open(arg[1],"rb");
--local txt = file:read('*a');
--print(txt);
local modm = {};
local len = 0;
local _break; --退出当前pat
for l in file:lines() do
    --print(l);
    if(l:find('^|'))then
        local i = 1;
        if(l:find('D%.%.'))then
            _break = true;
        end
        if(not _break)then
            --_brea
            for m in string.gmatch(l,"|(...........)")do
                --io.stdout:write('|',m);
                if(modm[i]==nil)then
                    modm[i] = {};
                end
                table.insert(modm[i],m);
                i = i + 1;
            end
            len = len + 1;
        end
        --io.stdout:write('\n');
    else
        _break = nil;
    end
end

local dump = {};
dump[2] = 1;

-- for k=1,len do
--     for kk,v in ipairs(modm)do
--         io.stdout:write('|',v[k]);
--     end
--     io.stdout:write('\n');
-- end

--导出单声道模式
function dumpSingleTrack(idx)
    local wait = 0;
    for k,row in ipairs(modm[idx]) do
        local node = row:sub(1,3);
        local insn = row:sub(4,5);
        local vol = row:sub(6,8);
        local effect = row:sub(9,11);
        --print("|",node,insn,vol);
        if(node=='...' and insn=='..' and vol=='...')then
            wait = wait + 1;
        else
            while(wait>255)do
                print(string.format('m_wait,%d,\n',255));
                wait = wait - 255;
            end
            print(string.format('m_wait,%d,\n',wait));
            if(insn~='..')then
                print(string.format('m_insn,%d,',tonumber(insn)));
            end
            if(node~='...')then
                node = node:gsub('-','_');
                node = node:gsub('#','s');
                node = 'm_'..node;
                print(string.format('%s,',node));
            end
            if(vol~='...')then
                print(string.format('m_vol,%d,',tonumber(vol:sub(2))));
            end

            wait = 1;
        end
    end
end

--导出多声道
function dumpTrack(chs)
    local wait = 0;
    if(not chs)then
        chs = {};
        for k,v in ipairs(modm)do
            table.inser(chs,k);
        end
    end

    --local offset = 1;
    for offset,v in ipairs(modm[1])do
        wait = wait + 1;
        for kk,idx in ipairs(chs)do
            local row = modm[idx][offset];
        --for k,row in ipairs(v)do
            local node = row:sub(1,3);
            local insn = row:sub(4,5);
            local vol = row:sub(6,8);
            local effect = row:sub(9,11);
            --print("|",node,insn,vol);
            if(node=='...' and insn=='..' and vol=='...')then
                --wait = wait + 1;
                --当前不执行任何事件
            else
                --需要等待
                if(wait>0)then
                    print(string.format('m_wait,%d,',wait));
                    wait = 0;
                else
                    --当前row
                end
                --切换到声道，执行事件
                print(string.format('m_setc,%d,',idx));
                if(insn~='..')then
                    print(string.format('m_insn,%d,',tonumber(insn)));
                end
                if(node~='...')then
                    node = node:gsub('-','_');
                    node = node:gsub('#','s');
                    node = 'm_'..node;
                    print(string.format('%s,',node));
                end
                if(vol~='...')then
                    print(string.format('m_vol,%d,',tonumber(vol:sub(2))));
                end
            end
        end
    end
end

--导出声道1,2,3
dumpTrack({1,2,3});

